package softtech.app.secretcaller;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class AdapterChatSms extends ArrayAdapter<DatabaseSMS>{
	
	List<DatabaseSMS> mListSms;
	Context mContext;
	DatabaseHandler db;
	private DatabaseContact mContact;
	
	AdapterChatSms(Context context , int textViewResourceId , ArrayList<DatabaseSMS> list){
		super(context, textViewResourceId);
		db = new DatabaseHandler(context);
		mContext = context;
		mListSms = list;
	}
	
	@Override
	public int getCount() {
		if(mListSms==null || mListSms.equals(null) || mListSms.size() == 0){
			return 1;
		}
		return mListSms.size();
	}

	@Override
	public DatabaseSMS getItem(int position) {
		if(mListSms==null || mListSms.equals(null) || mListSms.size() == 0){
			return null;
		}
		return mListSms.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(parent.getContext()==null){		
			return null;
		}
		
		if(mListSms==null || mListSms.equals(null) || mListSms.size()==0){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,parent, false);
			return convertView;
		}
		
		ViewHolder viewHolder=null;
		if(convertView==null){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			if(mListSms.get(position).getType() == DatabaseSMS.SEND){
				convertView = inflater.inflate(R.layout.item_sms_mine,parent, false);
			}else{
				convertView = inflater.inflate(R.layout.item_sms_their,parent, false);
			}
			
			viewHolder=new ViewHolder();
			viewHolder.tvContentSms	= (TextView) convertView.findViewById(R.id.tvContentSms);
			viewHolder.tvName	= (TextView) convertView.findViewById(R.id.tvName);
			viewHolder.tvTime  = (TextView) convertView.findViewById(R.id.tvTime);
					
			
						
			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvContentSms,viewHolder.tvContentSms);
			convertView.setTag(R.id.tvName,viewHolder.tvName);
			convertView.setTag(R.id.tvTime,viewHolder.tvTime);
			
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		
		viewHolder.tvContentSms.setText(mListSms.get(position).getContent());
		if(ToolsSaveFile.checkStringNotNull(mListSms.get(position).getDate(), 18)){
			viewHolder.tvTime.setText(mListSms.get(position).getDate().substring(11	, 16));
		}else{
			viewHolder.tvTime.setText("");
		}
		if(mListSms.get(position).getType() == DatabaseSMS.SEND){
			//viewHolder.tvName.setText(mListSms.get(position).getContent());
			
		}else if(mListSms.get(position).getType() == DatabaseSMS.RECEIVE){
			if(mContact!=null){
				viewHolder.tvName.setText(mContact.getContactName());
			}
			
			
		}
		
		
		return convertView;
	}

	static class ViewHolder{
		TextView tvContentSms , tvName , tvTime;
	}

	public DatabaseContact getContact() {
		return mContact;
	}

	public void setContact(DatabaseContact mContact) {
		this.mContact = mContact;
	}

	
	
}
